/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.prozezuak;

import igraf.ItzuliInterfazea;
import igraf.Panela;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import dBasea.Abokatua;
import dBasea.Istripu;

public class AbokatuEsleitu extends Panela {
	private Abokatua abok = new Abokatua();
	private Istripu istri = new Istripu();
	private static final long serialVersionUID = 5966395355263397317L;
	private JLabel zenbakia, abokatua;
	private JTextField nif;
	private JButton ok;
	private JButton ezeztatu;
	private JComboBox abokatuak;
	private JButton bilatu;

	public AbokatuEsleitu(String tituloa,
			final ItzuliInterfazea itzuliInterfazea) {
		super(tituloa);
		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout());

		/*
		 * JPanel arribb = new JPanel();
		 * arribb.setBackground(SystemColor.activeCaption);
		 * arri.setBackground(SystemColor.activeCaption); FlowLayout arrib = new
		 * FlowLayout(); arrib.setHgap(15); arrib.setVgap(10);
		 * arribb.setLayout(arrib); zer=new JTextField(15); data=new
		 * JLabel("Gaurko data:"); arribb.add(data); data.setFont(new
		 * Font("Arial", Font.PLAIN, 14)); arribb.add(zer); erdikoa.add(arribb);
		 */

		JPanel e = new JPanel();
		e.setBackground(SystemColor.activeCaption);

		// Erdiko panelak
		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoa.setLayout(new GridLayout(7, 1));

		JPanel g = new JPanel();
		g.setBackground(SystemColor.activeCaption);
		erdikoa.add(g);

		JPanel geg = new JPanel();
		geg.setBackground(SystemColor.activeCaption);
		erdikoa.add(geg);

		JPanel arri = new JPanel();
		arri.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arri = new FlowLayout();
		fl_arri.setHgap(15);
		fl_arri.setVgap(10);
		arri.setLayout(fl_arri);
		zenbakia = new JLabel("Istripu zenbakia:");
		zenbakia.setFont(new Font("Arial", Font.PLAIN, 14));
		nif = new JTextField(8);
		arri.add(zenbakia);
		arri.add(nif);

		bilatu = new JButton("Bilatu");
		bilatu.setIcon(new ImageIcon(AbokatuEsleitu.class
				.getResource("/Argazkia/lupa1r.png")));
		arri.add(bilatu);
		bilatu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					// eremu hutsak zerbait idatzi bada
					if (!zerbaitIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					}// existitzen bada istripua
					else if (!istri.dagoIstripua(nif.getText())) {
						JOptionPane.showMessageDialog(null,
								"Ez da istripu hori aurkitzen", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else {
						int istripu = Integer.parseInt(nif.getText());// istripua
																		// daukagu
						int hiria = istri.getHi(istripu);// hiria
															// del
															// istripu
															// anterior
						java.util.List<String> lista = abok
								.getAbokatuakHirian(hiria);// lista bat
															// tailer-hiria
						abokatuak.removeAllItems();
						if (lista != null) {
							for (String hiriaa : lista) {
								abokatuak.addItem(hiriaa);
							}
						}
						nif.setEnabled(false);
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}

		});

		erdikoa.add(arri);

		JPanel d = new JPanel();
		abokatuak = new JComboBox();
		d.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arrib = new FlowLayout();
		fl_arrib.setHgap(15);
		fl_arrib.setVgap(10);
		d.setLayout(fl_arrib);
		abokatua = new JLabel("Abokatuak hiri honetan");
		abokatua.setFont(new Font("Arial", Font.PLAIN, 14));
		d.add(abokatua);
		d.add(abokatuak);
		erdikoa.add(d);

		// beheko panela
		JPanel behekoa = new JPanel();
		FlowLayout flowLayout = (FlowLayout) behekoa.getLayout();
		flowLayout.setHgap(110);
		flowLayout.setVgap(10);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Abokatua esleitu istripu bati");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);

		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (!zerbaitIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					}

					else {
						Object ab = abokatuak.getSelectedItem();
						String hu = ab.toString();
						istri.aboIstripuEsleipena(
								Integer.parseInt(nif.getText()), hu);
						JOptionPane.showMessageDialog(null,
								"Abokatua esleituta " + nif.getText()
										+ " istripuarekin", "",
								JOptionPane.INFORMATION_MESSAGE);
						eremuakGarbitu();
						abokatuak.removeAllItems();
						nif.setEnabled(true);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		ezeztatu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				nif.setEnabled(true);
				ok.setEnabled(true);
				itzuliInterfazea.itzuliLehenPanela();
			}
		});

	}

	private boolean zerbaitIdatzi() {
		boolean aurki;
		aurki = nif.getText().length() > 0;
		return aurki;
	}

	@Override
	public void eremuakGarbitu() {
		abokatuak.removeAllItems();
		nif.setText("");
	}

}
